const host = 'https://meituan.thexxdd.cn/apin'

/* 
* 发起请求
*/
function requestapi(url,data={},method='GET',header={}){
	return new Promise((resolve,reject) => {
		uni.showLoading({
			title: '~'
		})
		uni.request({
			url:host+url,
			method,
			data,
			header,
			success:(res)=>{
				resolve(statushandle(res.data))
			},
			fail:(err)=>{
				reject(err)
				console.warn(err)
			},
			complete:()=>{
				uni.hideLoading()
			}
			
		})
	})
}
/**
* 请求判断操作
*/
function statushandle(res){
	return new Promise((resolve,reject) => {
		switch (res.statusCode){
			case 200:
				resolve(res.data)
				break;
			case 401:
				uni.navigateTo({url:'/symmetry/porch'})
				break;
			case 400:
				showtoast(res.data.msg)
				reject(400)
				break;
			case 404:
				reject(404)
				break;
			case 422:
				showtoast(res.data.msg)
				reject(res.statusCode)
				break;
			case 500:
				reject(500)
				showtoast('Internal Server Erro')
				break;
			case 501:
				reject(501)
				showtoast('Not Implemented')
				break;
			case 502:
				reject(502)
				showtoast('Bad Gateway')
				break;
			default:
				resolve(res)
				break;
		}	
	})
}
/**
 * 提示信息
*/
function showtoast(title){
	uni.showToast({
		title,
		icon:true,
		duration:1000
	})
}

export {
	requestapi
}
